package com.black.cat.jvm;

import com.black.cat.jvm.classpath.Classpath;

/**
 * @Author blackcat
 * @create 2021/8/11 13:43
 * @version: 1.0
 * @description:命令行工具
 */
public class Main {

    public static void main(String[] args) {
        String[] argv = {"-classpath","D:\\develop\\code\\jjvm\\jvm-02\\target\\classes","com.black.cat.jvm.MainTest"};
        Cmd cmd = Cmd.parse(argv);
        if (!cmd.ok || cmd.helpFlag) {
            System.out.println("Usage: <main class> [-options] class [args...]");
            return;
        }
        if (cmd.versionFlag) {
            System.out.println("java version \"1.8.0\"");
            return;
        }
        startJVM(cmd);
    }

    private static void startJVM(Cmd cmd) {
        System.out.printf("classpath:%s class:%s args:%s\n", cmd.classpath, cmd.getMainClass(), cmd.getAppArgs());

        Classpath classpath = new Classpath(null, cmd.classpath);
        try {
            //java.lang.Object
            String className = cmd.getMainClass().replace(".", "/");
            byte[] classData = classpath.readClass(className);
            System.out.println("classData：");
            for (byte b : classData) {
                //16进制输出
                System.out.print(String.format("%02x", b & 0xff) + " ");
            }
            System.out.println();
        } catch (Exception e) {
            System.out.println("Could not find or load main class ");
            e.printStackTrace();
        }
    }
}
